
import { React } from 'react';
export default function createStore(reducer, enhancer) {

  if(enhancer) { // 加强dispatch 支持函数 promise
    return enhancer(createStore)(reducer)
  }
  let crrentState
  let crrentListener = []

  function getState() {
    return crrentState
  }

  function dispatch(action) {
    crrentState = reducer(crrentState, action)
    crrentListener.forEach(listener => listener())
  }
  
  function subscribe(listener) {
    crrentListener.push(listener)
    return () => {
      const index = crrentListener.indexOf(listener)
      crrentListener.splice(index, 1)
    }
  }

  dispatch({type: '@REDUX/XXXXXXXXXXXXXXX'}) // 生成默认值

  return {
    getState,
    dispatch,
    subscribe
  }
}